package com.digitalchina.lxnsy.activity.old;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessExpert;
import com.digitalchina.lxnsy.api.BusinessMyFarm;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ExpertClassificationVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.widget.EditTextWithClear;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/12/14 14:14
 * 修改备注：
 * ================================================
 */
public class AddMyFarmActivity extends BaseActivity {

    @Bind(R.id.tv_classification)
    TextView tvClassification;
    @Bind(R.id.et_unit)
    EditTextWithClear etUnit;
    @Bind(R.id.tv_unit)
    TextView tvUnit;

    // 1级分类
    private List<ExpertClassificationVO> list1 = new ArrayList<>();
    // 2级分类
    private List<List<ExpertClassificationVO.ResearchFieldTwoStagesBean>> list2 = new ArrayList<>();
    // 3级分类
    private List<List<List<ExpertClassificationVO.ResearchFieldTwoStagesBean.ResearchFieldThreeStageVosBean>>> list3 = new ArrayList<>();
    // 选择器
    private OptionsPickerView pvOptions;

    private String code1, code2, code3;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_add_my_farm);
        setTitle("添加农场信息");
    }

    @Override
    public void initMember() {

        // 产品分类
        BusinessExpert.primaryClassificationAll(this, mHandler);

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_PRIMARY_CLASSIFICATION_ALL_SUCCESS:
                        if (pvOptions != null && pvOptions.isShowing())
                            return;
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_PRIMARY_CLASSIFICATION_ALL_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_SAVE_FARM_SUCCESS:
                        showToast("保存成功");
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case MSG.MSG_SAVE_FARM_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        list1 = FastJsonUtil.getListBean(object.toString(), ExpertClassificationVO.class);

        if (list1 != null && list1.size() > 0) {
            initClassification();
        }
    }

    private void initClassification() {

        // 临时2级分类
        List<List<ExpertClassificationVO.ResearchFieldTwoStagesBean.ResearchFieldThreeStageVosBean>> temp2;


        for (int i = 0; i < list1.size(); i++) {
            list2.add(list1.get(i).getResearchFieldTwoStages());
        }

        for (int i = 0; i < list1.size(); i++) {
            temp2 = new ArrayList<>();
            for (int j = 0; j < list1.get(i).getResearchFieldTwoStages().size(); j++) {
                // 临时3级分类 - 单层list
                List<ExpertClassificationVO.ResearchFieldTwoStagesBean.ResearchFieldThreeStageVosBean> temp3 = list1.get(i).getResearchFieldTwoStages().get(j).getResearchFieldThreeStageVos();
                temp2.add(temp3);
            }
            list3.add(temp2);
        }

        // 条件选择器初始化
        pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {

                String opt1 = list1.get(options1).getPickerViewText();
                String opt2 = list2.get(options1).get(option2).getPickerViewText();
                String opt3 = list3.get(options1).get(option2).get(options3).getPickerViewText();

                code1 = list1.get(options1).getCode();
                code2 = list2.get(options1).get(option2).getCode();
                code3 = list3.get(options1).get(option2).get(options3).getCode();

                tvClassification.setText(opt1 + " " + opt2 + " " + opt3);
                tvUnit.setText("（单位：" + list1.get(options1).getResearchFieldTwoStages().get(option2).getUnitName() + "）");

            }
        })
                .setSubmitText("确定")
                .setCancelText("取消")
                .setTitleText("作物种类")
                .setSubCalSize(16)//确定取消按钮大小
                .setLineSpacingMultiplier(1.7f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .setDividerColor(getResources().getColor(R.color.app_color))//设置分割线的颜色
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0, 0)  //设置默认选中项
                .build();

        pvOptions.setPicker(list1, list2, list3);

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rl_classification, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_classification:
                if (pvOptions != null) {
                    pvOptions.show();
                } else {
                    showToast("正在获取作物种类");
                }
                break;
            case R.id.btn_save:
                if (StringUtil.isStrEmpty(code1)) {
                    showToast("请选择分类");
                    return;
                }
                if (StringUtil.isEditEmpty(etUnit)) {
                    showToast("请填写规模");
                    return;
                }
                BusinessMyFarm.saveFam(AddMyFarmActivity.this, code1, code2, code3, etUnit.getText().toString(), mHandler);
                break;
        }
    }
}
